package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.constant.MessageConstant;
import com.atguigu.dao.MemberDao;
import com.atguigu.dao.OrderDao;
import com.atguigu.dao.OrderSettingDao;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Member;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.OrderService;
import com.atguigu.service.OrderSettingService;
import com.atguigu.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderSettingDao orderSettingDao;
    @Autowired
    OrderDao orderDao;
    @Autowired
    MemberDao memberDao;

    @Override
    public Result order(Map<String, String> map) throws Exception {
//        1. 判断当前的日期是否可以预约(根据orderDate查询t_ordersetting, 能查询出来可以预约;查询不出来,不能预约)
        String orderDate = map.get("orderDate");
        String setmealId = map.get("setmealId");

        OrderSetting orderSetting = orderSettingDao.findOrderSettingByOrderDate(orderDate);

        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
//        2. 判断当前日期是否预约已满(判断reservations（已经预约人数）是否等于number（最多预约人数）)
        int reservations = orderSetting.getReservations();
        int number = orderSetting.getNumber();
        if (reservations >= number) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }

//        3. 判断是否是会员(根据手机号码查询t_member)
        String telephone = map.get("telephone");
        Member member = memberDao.findMemberByTelephone(telephone);
//        - 如果不是会员(不能够查询出来) ,自动注册为会员(直接向t_member插入一条记录)
        if (member == null) {
            member = new Member();
            // 如果不是会员：注册会员，向会员表中添加数据
            member = new Member();
            member.setName(map.get("name"));
            member.setSex(map.get("sex"));
            member.setPhoneNumber(map.get("telephone"));
            member.setIdCard(map.get("idCard"));
            member.setRegTime(new Date()); // 会员注册时间，当前时间
            memberDao.add(member); // 需要主键回填
        } else {
//            - 如果是会员(能够查询出来), 防止重复预约(根据member_id,orderDate,setmeal_id查询t_order)
            Integer memberId = member.getId();

            Order order = orderDao.findByCondition(memberId, orderDate, setmealId);
            if (order != null) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }
//        4.进行预约
//                - 向t_order表插入一条记录
         Date date = DateUtils.parseString2Date(orderDate);
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(date);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setOrderType(map.get("orderType"));
        order.setSetmealId(Integer.parseInt(setmealId));
        orderDao.add(order);
//                - t_ordersetting表里面预约的人数reservations+1
        orderSetting.setReservations(reservations + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order);
    }

    @Override
    public Map findById4Detail(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if(map != null){
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
            return map;
        }
        return map;
    }
}
